package com.baichang.taoshunda.service.impl;

import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.ScoreDao;
import com.baichang.taoshunda.dao.ScoreGoodsDao;
import com.baichang.taoshunda.dao.ScoreListDao;
import com.baichang.taoshunda.entity.ScoreGoods;
import com.baichang.taoshunda.service.ParamsService;
import com.baichang.taoshunda.service.ScoreGoodsService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/18 0018.
 */
@Service
public class ScoreGoodsServiceImpl implements ScoreGoodsService {
    @Autowired
    private ScoreGoodsDao scoreGoodsDao;
    @Autowired
    private ScoreDao scoreDao;

    @Autowired
    private ScoreListDao scoreListDao;
    @Autowired
    private ParamsService paramsService;

    /**
     * 获取所有的积分商品
     *
     * @param nowPage  分页当前页
     * @param pageSize 每页条数
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getScoreGoodsList(Integer nowPage, Integer pageSize, Integer userId) {
        Map<String, Object> map = new HashedMap();
        List<Map<String, Object>> mapList = scoreGoodsDao.getScoreGoodsList(nowPage, pageSize);
        int score = scoreDao.getScoreByUserId(userId);
        map.put("score", score);
        map.put("mapList", mapList);
        return map;
    }

    /**
     * @param id 商品编号
     * @return
     */
    @Override
    public Map<String, Object> getScoreGoodsById(Integer id) {
        return scoreGoodsDao.getScoreGoodsById(id);
    }

    /**
     * 变动库存
     *
     * @param goodsId 商品编号
     * @param number  库存量
     * @return
     */
    @Override
    public boolean scoreGoodsStock(int goodsId, int number) {
        return scoreGoodsDao.scoreGoodsStock(goodsId, number);
    }


    /**
     * @param goodsId
     * @return
     */
    @Override
    public int getScoreGoodsStock(int goodsId) {
        return scoreGoodsDao.getScoreGoodsStock(goodsId);
    }

    /**
     * 查询商品积分
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryGoodsScore(String name, int nowPage, int pageSize) {
        return scoreGoodsDao.queryGoodsScore(name, nowPage, pageSize);
    }

    /**
     * 添加积分商品
     *
     * @param goodsScore
     * @param hotLine
     * @param needScore
     * @param marketPrice
     * @param stock
     * @param goodsPic
     * @param remark
     * @return
     */
    @Override
    public long addGoodsScore(String goodsScore, String hotLine, String needScore, BigDecimal marketPrice, Integer stock, String goodsPic, String listPic, String remark) {
        hotLine = paramsService.getParamsValue(28, 26).get("value").toString();
        ScoreGoods scoreGoods = new ScoreGoods();
        scoreGoods.setScoreGoodsName(goodsScore);
        scoreGoods.setHotLine(hotLine);
        scoreGoods.setNeedScore(needScore);
        scoreGoods.setMarketPrice(marketPrice);
        scoreGoods.setStock(stock);
        scoreGoods.setGoodsPic(goodsPic);
        scoreGoods.setListPic(listPic);
        scoreGoods.setRemark(remark);
        scoreGoods.setCreated(new Date());
        scoreGoods.setState(1);
        return scoreGoodsDao.save(scoreGoods);
    }

    /**
     * 修改积分商品
     *
     * @param id
     * @param goodsScore
     * @param hotLine
     * @param needScore
     * @param marketPrice
     * @param stock
     * @param goodsPic
     * @param remark
     * @return
     */
    @Override
    public int updateGoodsScore(Integer id, String goodsScore, String hotLine, String needScore, BigDecimal marketPrice, Integer stock, String goodsPic, String listPic, String remark) {
        Map map = new HashedMap();
        map.put("scoreGoodsName", goodsScore);
//        map.put("hotLine", hotLine);
        map.put("needScore", needScore);
        map.put("marketPrice", marketPrice);
        map.put("stock", stock);
        map.put("goodsPic", goodsPic);
        map.put("listPic", listPic);
        map.put("remark", remark);
        return scoreGoodsDao.update(map, id);
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateState(Integer id, Integer state) {
        return scoreGoodsDao.updateState(id, state);
    }

    /**
     * 送货状态
     *
     * @param id
     * @param sendGoods
     * @return
     */
    @Override
    public int updateIsSendGoods(Integer id, Integer sendGoods) {
        return scoreListDao.updateIsSendGoods(id, sendGoods);
    }
}
